﻿using System;
using System.IO;
using System.Collections.Generic;
using Petra.Gis.Geometry;
using Petra.Gis.Geometry.Support;
//using Petra.Gis.Geometry.GridSupport;
using Petra.Gis.DataServices;


namespace Petra.Gis.DataServices.GDO
{
  public unsafe partial class GeometryFactory
  {


    private static GeometryBase binaryToGrid(byte[] vData, int arrayOffset, System.Type parentGeomType)
    {
      MemoryStream m = new MemoryStream(vData);
      BinaryReader br = new BinaryReader(m);
      br.BaseStream.Seek(arrayOffset + 16, SeekOrigin.Begin);

      //CoordinateList Coords = new CoordinateList(ref vData, arrayOffset + 16, 1, Dimension.dim3D);

      //load matrix
      double[] tmpMatrix = new double[16];
      for (int i = 0; i < 16; i++)
        tmpMatrix[i] = br.ReadDouble();


      //tmpMatrix = new ImageTransformMatrix(matrix);

      // load bounding rectangle, ingr indian assholes, everything is different
      // 2---------------1
      // |
      // |
      // |
      // 3---------------4
      double[] bounds = new double[12];
      for (int i = 0; i < 12; i++)
        bounds[i] = br.ReadDouble();

      //oGeom.CoordinateList = new CoordinateList(Dimension.dim3D, bounds);

      int strLen = br.ReadInt32();
      Uri filePath = new Uri(System.Text.Encoding.Unicode.GetString(br.ReadBytes(strLen*2)));


      //todo: repair boundingBox!!!
      Grid oGeom = new Grid(GridData.Create(filePath, new BoundingBox(bounds[6],bounds[7],bounds[0],bounds[1])));

      return oGeom;
    }





    private static byte[] gridToBinary(GeometryBase geometry)
    {
      int lBlobSize;
      string strPath;
      Guid gGeomGUID;

      Grid _tmpGeom = (Grid)geometry;

      gGeomGUID = getGuid[GeometryType.SpatialReferencedImage];

      if (_tmpGeom.DataLocation.IsFile)
        strPath = _tmpGeom.DataLocation.LocalPath;
      else
        strPath = _tmpGeom.DataLocation.ToString();

      lBlobSize = 244 + strPath.Length;

      System.IO.MemoryStream oIMStream = new System.IO.MemoryStream(lBlobSize);
      System.IO.BinaryWriter mBWriter = new System.IO.BinaryWriter(oIMStream);

      oIMStream.Position = 0;
      mBWriter.Write(gGeomGUID.ToByteArray());

      double[] tmpMatrix = new double[16];
      //todo: correct !!!
      tmpMatrix[1] = _tmpGeom.BoundingBox.Left;
      tmpMatrix[2] = _tmpGeom.BoundingBox.Top;
      tmpMatrix[0] = _tmpGeom.BoundingBox.Width - _tmpGeom.Data.Frames[0].Width;
      tmpMatrix[5] = _tmpGeom.BoundingBox.Height - _tmpGeom.Data.Frames[0].Height;

      foreach (double value in tmpMatrix)
        mBWriter.Write(value);


      //chage BoundingBox to 4 point double[]
      double[] tmpCl = new double[12]
      { 
      _tmpGeom.BoundingBox.Left, _tmpGeom.BoundingBox.Top, 0,
      _tmpGeom.BoundingBox.Right, _tmpGeom.BoundingBox.Top, 0,
      _tmpGeom.BoundingBox.Right, _tmpGeom.BoundingBox.Bottom, 0,
      _tmpGeom.BoundingBox.Left, _tmpGeom.BoundingBox.Bottom, 0
      };
      foreach (double value in tmpCl)
        mBWriter.Write(value);

      //write as UTF-16
      mBWriter.Write((Int32)(strPath.Length * 2));
      mBWriter.Write(System.Text.Encoding.Unicode.GetBytes(strPath));

      return oIMStream.ToArray();
    }



  }
}
